<template>
  <div class="component-distribution-distributionUserWalletBill">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('distributionMsg.nickName') + ':'">
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item :label="$t('distributionMsg.distriTelPhone') + ':'">
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.distriTelPhone')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch()"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con distr-user-allet-bill-tbale">
        <el-table
          ref="distributionUserWalletBillTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <!-- 分销员昵称 -->
          <el-table-column
            width="auto"
            prop="nickName"
            :label="$t('distribUserWallet.distributorNickname')"
          >
            <template #default="scope">
              {{ scope.row.distributionUser ? scope.row.distributionUser.nickName : '' }}
            </template>
          </el-table-column>
          <!-- 分销员手机号 -->
          <el-table-column
            width="auto"
            prop="userMobile"
            :label="$t('distribUserWallet.mobileTelNumber')"
          >
            <template #default="scope">
              {{ scope.row.distributionUser ? scope.row.distributionUser.userMobile : '' }}
            </template>
          </el-table-column>
          <!-- 待结算金额变更数额 -->
          <el-table-column
            width="auto"
            prop="unsettledAmount"
            :label="$t('distribIncome.pschange')"
          >
            <template #default="scope">
              {{ scope.row.unsettledAmount }}
            </template>
          </el-table-column>
          <!-- 可提现金额变更数额 -->
          <el-table-column
            width="auto"
            prop="settledAmount"
            :label="$t('distribIncome.wachange')"
          >
            <template #default="scope">
              {{ scope.row.settledAmount }}
            </template>
          </el-table-column>
          <!-- 已失效金额变更数额 -->
          <el-table-column
            width="auto"
            prop="invalidAmount"
            :label="$t('distribIncome.iachange')"
          >
            <template #default="scope">
              {{ scope.row.invalidAmount }}
            </template>
          </el-table-column>
          <!-- 积累收益变更数额 -->
          <el-table-column
            width="auto"
            prop="addupAmount"
            :label="$t('distribIncome.aichange')"
          >
            <template #default="scope">
              {{ scope.row.addupAmount }}
            </template>
          </el-table-column>
          <!-- 备注 -->
          <el-table-column
            width="auto"
            prop="remarks"
            :label="$t('publics.remark')"
          >
            <template #default="scope">
              {{ scope.row.remarks }}
            </template>
          </el-table-column>
          <!-- 创建时间 -->
          <el-table-column
            width="auto"
            prop="createTime"
            :label="$t('sys.creationTime')"
          >
            <template #default="scope">
              {{ scope.row.createTime }}
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
onMounted(() => {
  getDataList()
})

let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataListLoading = ref(false)
const searchForm = ref({})
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/distribution/distributionUserWalletBill/pageAndUser'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam == null ? page.currentPage : pageParam.currentPage,
      size: pageParam == null ? page.pageSize : pageParam.pageSize
    }, tempSearchForm))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    dataListLoading.value = false
  })
}

const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}
</script>
